package edu.mju.eduuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.mju.commonutils.EncryptUtil;
import edu.mju.commonutils.JwtUtils;
import edu.mju.eduuser.entity.StudyMember;
import edu.mju.eduuser.entity.vo.UserRegisterVo;
import edu.mju.eduuser.mapper.StudyMemberMapper;
import edu.mju.eduuser.service.StudyMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.mju.servicebase.exception.StuException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xiaoxin
 * @since 2020-09-27
 */
@Service
public class StudyMemberServiceImpl extends ServiceImpl<StudyMemberMapper, StudyMember> implements StudyMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    /**
     * 登录方法
     * @param studyMember 登录对象
     * @return token
     */
    @Override
    public String login(StudyMember studyMember) {
        //获取登录手机号和密码
        String mobile = studyMember.getMobile();
        String password = studyMember.getPassword();
        //判断为null 抛出异常
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new StuException(20001,"手机号或密码为空，登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<StudyMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        StudyMember mobieMember = baseMapper.selectOne(wrapper);
        if(mobieMember == null){
            throw new StuException(20001,"手机号不存在，登录失败");
        }

        //判断密码 使用MD5加密
        if(!EncryptUtil.md5(password).equals(mobieMember.getPassword())){
//        if(!password.equals(mobieMember.getPassword())){

                throw new StuException(20001,"密码错误，登陆失败");
        }

        //判断是否禁用
        if(mobieMember.getIsDeleted()){
            throw new StuException(20001,"账号被禁用，登录失败");
        }

        //登录成功
        //生成token
        String jwtToken = JwtUtils.getJwtToken(mobieMember.getId(), mobieMember.getNickname());


        return jwtToken;
    }


    /**
     * 注册方法
     * @param userRegisterVo 用户注册信息
     */
    @Override
    public void register(UserRegisterVo userRegisterVo) {
        //获取用户注册信息
        String nickname = userRegisterVo.getNickname();
        String mobile = userRegisterVo.getMobile();
        String password = userRegisterVo.getPassword();
        String securitycode = userRegisterVo.getSecuritycode();

        //处理非空
        if(StringUtils.isEmpty(nickname)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)||
        StringUtils.isEmpty(securitycode)){
            throw new StuException(20001,"注册失败，有空参数！");
        }

        //获取Redis中的验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        //判断验证码
        if(!securitycode.equals(redisCode)){
            throw new StuException(20001,"注册失败，验证码不一致！");
        }

        //判断手机号是否重复
        QueryWrapper<StudyMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer result = baseMapper.selectCount(wrapper);
        if(result > 0 ){
            throw new StuException(20001,"注册失败，手机号已存在！");
        }

        //添加数据到数据库
        StudyMember studyMember = new StudyMember();
        studyMember.setMobile(mobile);
        studyMember.setNickname(nickname);
        //自定义加密密码
        studyMember.setPassword(EncryptUtil.md5(password));
        studyMember.setIsDisabled(false);
        studyMember.setAvatar("https://mju-sdy.oss-cn-hangzhou.aliyuncs.com/defaultUserAvatar/defaultAvatar.jpg");
        baseMapper.insert(studyMember);

    }

    @Override
    public StudyMember getOpenIdMember(String openid) {
        QueryWrapper<StudyMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        StudyMember studyMember = baseMapper.selectOne(wrapper);
        return studyMember;
    }

    /**
     * 根据日期查询某一天的注册人数
     * @param day 日期
     * @return 注册人数
     */
    @Override
    public Integer countRegister(String day) {
        return baseMapper.countRegister(day);
    }

    @Override
    public StudyMember getGiteeIdMember(String giteeId) {
        QueryWrapper<StudyMember> wrapper = new QueryWrapper<>();
        wrapper.eq("giteeid",giteeId);
        StudyMember studyMember = baseMapper.selectOne(wrapper);
        return studyMember;
    }


}
